package 数组;

public class 移除元素 {
    public static void main(String[] args) {
        int[] nums = new int[]{0,1,2,2,3,0,4,2};
        int val = 2;
        System.out.println(removeElement(nums, val));
    }

    public static int removeElement(int[] nums, int val) {
        int p1 = 0;
        int p2 = nums.length - 1;

        //nums = [1,3,2,2,3], val = 3
        //
        while (p1 <= p2) {
            if (nums[p1] != val) {
                p1++;
            } else if (nums[p2] == val) {
                p2--;
            } else {
                int temp = nums[p1];
                nums[p1] = nums[p2];
                nums[p2] = temp;

                p1++;
                p2--;
            }
        }
        return p1;
    }
}
